package com.studyweb.controller;

import com.studyweb.pojo.entity.PaperResult;
import com.studyweb.pojo.vo.Result;
import com.studyweb.service.PaperResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/api/paper-result")
@CrossOrigin
public class PaperResultController {
    
    private static final Logger logger = LoggerFactory.getLogger(PaperResultController.class);
    
    @Autowired
    private PaperResultService paperResultService;
    
    /**
     * 创建试卷测验结果
     */
    @PostMapping
    public Result<PaperResult> createPaperResult(@RequestBody PaperResult paperResult) {
        try {
            logger.info("开始创建试卷测验结果: userId={}, paperId={}", paperResult.getUserId(), paperResult.getPaperId());
            logger.debug("试卷测验结果详情: {}", paperResult);
            
            // 检查必要字段
            if (paperResult.getUserId() == null || paperResult.getPaperId() == null) {
                logger.warn("创建试卷测验结果失败: 缺少必要字段 userId={}, paperId={}", paperResult.getUserId(), paperResult.getPaperId());
                return Result.error("缺少必要字段");
            }
            
            PaperResult createdPaperResult = paperResultService.createPaperResult(paperResult);
            logger.info("试卷测验结果创建成功: id={}", createdPaperResult.getId());
            return Result.success("试卷测验结果创建成功", createdPaperResult);
        } catch (Exception e) {
            logger.error("试卷测验结果创建失败", e);
            return Result.error("试卷测验结果创建失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID获取试卷测验结果
     */
    @GetMapping("/{id}")
    public Result<PaperResult> getPaperResultById(@PathVariable Long id) {
        try {
            logger.info("开始获取试卷测验结果: id={}", id);
            PaperResult paperResult = paperResultService.getPaperResultById(id);
            if (paperResult != null) {
                logger.info("获取试卷测验结果成功: id={}", id);
                return Result.success("获取成功", paperResult);
            } else {
                logger.warn("试卷测验结果不存在: id={}", id);
                return Result.error(404, "试卷测验结果不存在");
            }
        } catch (Exception e) {
            logger.error("获取试卷测验结果失败", e);
            return Result.error("获取试卷测验结果失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID和试卷ID获取试卷测验结果
     */
    @GetMapping("/user/{userId}/paper/{paperId}")
    public Result<List<PaperResult>> getPaperResultsByUserIdAndPaperId(@PathVariable Long userId, 
                                                                @PathVariable Long paperId) {
        try {
            logger.info("开始获取试卷测验结果: userId={}, paperId={}", userId, paperId);
            List<PaperResult> paperResults = paperResultService.getPaperResultsByUserIdAndPaperId(userId, paperId);
            logger.info("获取试卷测验结果成功: userId={}, paperId={}, 结果数量={}", userId, paperId, paperResults.size());
            return Result.success("获取成功", paperResults);
        } catch (Exception e) {
            logger.error("获取试卷测验结果失败", e);
            return Result.error("获取试卷测验结果失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID获取试卷测验结果
     */
    @GetMapping("/user/{userId}")
    public Result<List<PaperResult>> getPaperResultsByUserId(@PathVariable Long userId) {
        try {
            logger.info("开始获取试卷测验结果: userId={}", userId);
            List<PaperResult> paperResults = paperResultService.getPaperResultsByUserId(userId);
            logger.info("获取试卷测验结果成功: userId={}, 结果数量={}", userId, paperResults.size());
            return Result.success("获取成功", paperResults);
        } catch (Exception e) {
            logger.error("获取试卷测验结果失败", e);
            return Result.error("获取试卷测验结果失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新试卷测验结果
     */
    @PutMapping
    public Result<PaperResult> updatePaperResult(@RequestBody PaperResult paperResult) {
        try {
            logger.info("开始更新试卷测验结果: id={}", paperResult.getId());
            PaperResult updatedPaperResult = paperResultService.updatePaperResult(paperResult);
            logger.info("试卷测验结果更新成功: id={}", updatedPaperResult.getId());
            return Result.success("试卷测验结果更新成功", updatedPaperResult);
        } catch (Exception e) {
            logger.error("试卷测验结果更新失败", e);
            return Result.error("试卷测验结果更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除试卷测验结果
     */
    @DeleteMapping("/{id}")
    public Result<Void> deletePaperResult(@PathVariable Long id) {
        try {
            logger.info("开始删除试卷测验结果: id={}", id);
            paperResultService.deletePaperResult(id);
            logger.info("试卷测验结果删除成功: id={}", id);
            return Result.success(null);
        } catch (Exception e) {
            logger.error("试卷测验结果删除失败", e);
            return Result.error("试卷测验结果删除失败: " + e.getMessage());
        }
    }
}